package com.xyzh05.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.xyzh05.service.MaterialInfoManager;
import com.xyzh05.service.SchoolManager;
import com.xyzh05.util.PageBean;
import com.xyzh05.model.MaterialInfo;
import com.xyzh05.model.School;
import com.xyzh05.webapp.action.BaseAction;

import java.util.List;

public class SchoolAction extends BaseAction implements Preparable {
	
    /**
	 * 
	 */
	private static final long serialVersionUID = 12312312312312L;
	private SchoolManager schoolManager;
    private List<School> schools;
	private School school;
    private Long id;
    private String query;
    private PageBean pageBean;
    public void setSchoolManager(SchoolManager schoolManager) {
        this.schoolManager = schoolManager;
    }

    public List<School> getSchools() {
        return schools;
    }



	/**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String schoolId = getRequest().getParameter("school.id");
            if (schoolId != null && !schoolId.equals("")) {
                school = schoolManager.get(new Long(schoolId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
    	pageBean = schoolManager.findList(pageBean, school);
        return "list" ;
    }

    public String initSelectSchool() {
    	schools = schoolManager.getAll();
        return "initSelectSchool" ;
    }
    
    public void setId(Long id) {
        this.id = id;
    }

    public School getSchool() {
        return school;
    }

    public void setSchool(School school) {
        this.school = school;
    }

    public String delete() {
        schoolManager.remove(school.getId());
        saveMessage(getText("school.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            school = schoolManager.get(id);
        } else {
            school = new School();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (school.getId() == null);

        schoolManager.save(school);

        String key = (isNew) ? "school.added" : "school.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
    
    public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public SchoolManager getSchoolManager() {
		return schoolManager;
	}

	public void setSchools(List schools) {
		this.schools = schools;
	}
}